import tkinter as tk
import tkinter.ttk as ttk
import sys
import os
from tkinter.filedialog import (askopenfilename,askopenfilenames,askdirectory,asksaveasfilename)
sys.path.append("..")
sys.path.extend([os.path.join(root, name) for root, dirs, _ in os.walk("../") for name in dirs])
from Netlayer.FileSender import *
from Netlayer.FileReceiver import *
from threading import Thread
from Config.conf import *

class FileSenderGUI:
    def __init__(self,geometry,addr):
        print("FileSender GUI init, info:%s"%addr)
        self.win = tk.Tk()
        self.win.geometry(geometry)
        self.addr = addr
        self.win.title("与%s的连接"%self.addr["username"])
        
        # 标签显示地址信息
        self.addrLabel = tk.Label(self.win,text="hisName:%s  hisAddr:%s"%(self.addr["username"],self.addr["addr"]),
                                  width=40,height=2,bg="green",font=("Helvetica",16))
        self.addrLabel.place(x=160,y=10)
        # 聊天对话框
        self.ChatFrame = tk.Text(self.win,height=30,width=108)
        self.ChatFrame.place(x=20,y=80)

        # 进度条：横排 长度200 起点移至终点
        self.progressbar = ttk.Progressbar(self.win, orient=tk.HORIZONTAL, length=700, mode='determinate')
        self.progressbar.place(x=50,y=500)

        self.speedShow = tk.Label(self.win,width=25,height=1,text="传输速度:0Bytes/s")
        self.speedShow.place(x=600,y=530)

        # 发送文字按钮
        self.sendWordButton = tk.Button(self.win,text="发送文字",height=2,width=20,command=self.sendWord)
        self.sendWordButton.place(x=50,y=530)

        # 发送文件按钮
        self.sendFileButton = tk.Button(self.win,text="发送文件",height=2,width=20,command=lambda:self.sendFile(self.addr['addr']))
        self.sendFileButton.place(x=250,y=530)

        # 文件接收器线程
        self.fileReceiveThread = Thread(target=lambda:FileReceiver(myIp,913,receive_save_path))
        self.fileReceiveThread.daemon = True
        self.fileReceiveThread.start()

        time.sleep(1)
        # 文件发送线程
        self.path = ''
        self.canSend = False
        # self.info = {"canSend":False,"path":self.path,"addr":self.addr,"bar":self.progressbar,"speed":self.speedShow}

        self.FileSendThread = Thread(target= lambda:FileSender(parent=self))
        self.FileSendThread.daemon = True
        self.FileSendThread.start()

        # 退出按钮
        self.ExitButton = tk.Button(text="退出",height=2,width=20,command=self.Exit)
        self.ExitButton.place(x=450,y=530)

        # 退出标识
        self.ExitFlag = False

        self.MainLoop()
        
    
    def sendFile(self,addr):
        path = askopenfilename()
        print("select file:%s"%path)
        self.path = path
        self.canSend = True
        
        # self.FileSender = FileSender(path,addr,self.progressbar,self.speedShow)
        # self.FileSender.send(1024)
    
    def sendWord(self,word):
        
        return
    
    def Exit(self):
        self.win.destroy()
    
    def MainLoop(self):
        self.win.mainloop()

if __name__ == '__main__':
    myGUI = FileSenderGUI("800x600",{"username":"yu","addr":("127.0.0.1",913)})
    myGUI.MainLoop()
